package org.example.chatroom.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.val;
import org.example.chatroom.common.pojo.dataobject.Friend;
import org.example.chatroom.common.pojo.dataobject.UserInfo;
import org.example.chatroom.common.pojo.response.FriendResponse;
import org.example.chatroom.mapper.FriendMapper;
import org.example.chatroom.mapper.UserMapper;
import org.example.chatroom.service.FriendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-06-22
 * Time: 23:58
 */
@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<FriendResponse> getfriendlist(Integer userId) {
        //首先获得id列表
        QueryWrapper<Friend> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Friend::getUserId,userId);
        List<Friend> friends = friendMapper.selectList(queryWrapper);


        List<FriendResponse> friendResponses = friends.stream().map(friend -> {
            FriendResponse friendResponse = new FriendResponse();
            QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper();
            queryWrapper1.lambda().eq(UserInfo::getId,friend.getFriendId());
            UserInfo userInfo = userMapper.selectOne(queryWrapper1);
            BeanUtils.copyProperties(userInfo,friendResponse);
            return friendResponse;
        }) .collect(Collectors.toList());

        return friendResponses;
    }
}
